package cz.swosh.stomp.ejb;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author swosh
 */
@Stateless
@Local(value = Postman.class)
@Remote(value = PostmanRemote.class)
public class PostmanBean implements Postman, PostmanRemote {

    @Resource(name = "stomp_mail")
    Session session;
    protected Logger log = Logger.getLogger(getClass().getName());

    public boolean  sendActivationMail(String recipient, String aktHash) {


        String subject = "Aktivace";
        // TODO change domain implicit
        String content = "Aktivace <br/><br/>Pro úspěšné dokončení registrace klikněte"
                + " <a href=\"http://localhost:8080/stompear-web/activation?aktHashHex=" + aktHash + "\">zde</a>";


        Message message = new MimeMessage(session);

        try {
            InternetAddress to[] = new InternetAddress[1];
            to[0] = new InternetAddress(recipient);
            message.setRecipients(Message.RecipientType.TO, to);
            message.setSubject(subject);
            message.setContent(content, "text/html;charset=UTF-8");
            Transport.send(message);
        } catch (AddressException ex) {
            log.log(Level.SEVERE, null, ex);
            return false;
        } catch (MessagingException ex) {
            log.log(Level.SEVERE, null, ex);
            return false;
        }
return true;
    }
}
